/*
 * jz_json.c
 *
 * Copyright (C) 2014 Ingenic Semiconductor Co.,Ltd
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys_wrap/jz_json.h>
#include <json.h>
#ifdef DMALLOC
#include <imp/dmalloc.h>
#endif

//Managing the settings
int json_func(const char *debug)
{
	int result = -1, ch;
	char *text = NULL, tmp[200], *debug_check/*, *ret*/;
	json_t *root = NULL, *entry, *label,/* *value,*/ *subtree;

	debug_check = debug;

	if(!access("/tmp/Settings.jz",F_OK))
	{
		FILE *fp = fopen("/tmp/Settings.jz", "r");
		ch = getc(fp);
		int i=0;

		//Retrieve the current infos from the json file
		while(ch != EOF)
		{
			tmp[i] = ch;
			ch = getc(fp);
			i++;
		}
	   	tmp[i] = '\0';
		text = tmp;

		if(strlen(tmp) <= 3)
		{
			//root
			root = json_new_object();

			subtree = json_init("Label","Value");

			//Add subtree to root
			json_insert_child(root, subtree);

			//String format
			json_tree_to_string(root, &text);

			//Cleaning
			json_free_value(&root);
			json_parse_document(&root, text);

		}
		else{
			//Pass the retrieved infos to the json library
			json_parse_document(&root, text);
			json_tree_to_string(root, &text);
		}

		fclose(fp);
	}
	else{
		//Create the json object
		root = json_new_object();   //root

		subtree = json_init("Label","Value");

		//Add subtree to root
		json_insert_child(root, subtree);

		//String format
		json_tree_to_string(root, &text);

		//Cleaning
		json_free_value(&root);
		json_parse_document(&root, text);
	}

	text = json_format_string(text);
	//Pass the json infos to the the file
	FILE *fp = fopen("/tmp/Settings.jz", "w");
	fprintf(fp, "%s\n", text);

	//Retrieve the value of the "debug_check" string
	entry = json_find_first_label(root, "entry");
	label = json_find_first_label(entry->child, "Label");

	while(strcmp(label->text," ") != 0)
	{
		if(strcmp(label->text,debug_check)==0){
			debug_check = label->text;
			break;
		}
		label = label->next;
	}
	debug_check = label->text;

	label = json_find_first_label(entry->child, debug_check);

	//Make a decision
	if(strcmp(label->child->text, "On") == NULL)
	{
		result = 0;
		printf("[System Wrapper]: %s is enabled!!!\n", debug_check);
	}
	else if(strcmp(label->child->text, "Off") == NULL)
	{
		result = 1;
		printf("[System Wrapper]: %s is disabled!!!\n", debug_check);
	}
	else{
		result = -1;
		if(strcmp(label->child->text, " ") == NULL)
			printf("[System Wrapper]: Nothing is selected!!!\n");
		else
			printf("[System Wrapper]: Please check your json file!!!\n");
	}

	//Cleaning
	fclose(fp);
	free(text);
	json_free_value(&root);

	return result;
}

json_t *json_init(const char *Label1, const char *Value1)
{
	json_t *entry, *label, *value;

	//New object
	entry = json_new_object();

	//"Head": Input infos(label<-value)
	label = json_new_string(Label1);
	value = json_new_string(Value1);
	json_insert_child(label, value);
	json_insert_child(entry, label);

	//"Tail": Input infos(label<-value)
	label = json_new_string(" ");
	value = json_new_string(" ");
	json_insert_child(label, value);
	json_insert_child(entry, label);

	//New string for the object "entry"
	label = json_new_string("entry");

	//"entry": insert object to label
	json_insert_child(label, entry);

	return label;
}
